﻿//--------------------------------------------------------------------------------------------------------------------- 
// <copyright file="StarredLinkerPlugin.cs" company="Bit Tappers">
//   Copyright by Alvin Ashcraft 2009 - Bit Tappers.
// </copyright>
// <summary>
//   Defines the StarredLinkerPlugin type.
// </summary>
//---------------------------------------------------------------------------------------------------------------------
namespace AlvinAshcraft.LinkBuilder
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.ServiceModel.Syndication;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using WindowsLive.Writer.Api;

    /// <summary>
    /// WLW Plugin to generate links from a Google Reader ATOM feed.
    /// </summary>
    [WriterPluginAttribute("61B83824-ADEA-401d-86A1-87282C425E37", "Link Builder Plugin", ImagePath = "MountainDew-canrss1.png", PublisherUrl = "http://www.alvinashcraft.com", Description = "A plugin to insert Google Starred items as links.", HasEditableOptions = true)]
    [InsertableContentSourceAttribute("Insert Starred Items As Links")]
    public class StarredLinkerPlugin : ContentSource
    {
        /// <summary>
        /// Stores the options for the plugin.
        /// </summary>
        PluginSettings _options;

        /// <summary>
        /// Initializes the specified plugin options.
        /// </summary>
        /// <param name="pluginOptions">The plugin options.</param>
        public override void Initialize(IProperties pluginOptions)
        {
            base.Initialize(pluginOptions);
            _options = new PluginSettings(pluginOptions);
        }

        /// <summary>
        /// Edits the options.
        /// </summary>
        /// <param name="dialogOwner">The dialog owner.</param>
        public override void EditOptions(IWin32Window dialogOwner)
        {
            using (var of = new OptionsForm(_options))
            {
                of.ShowDialog(dialogOwner);
            }
        }

        /// <summary>
        /// Creates the content.
        /// </summary>
        /// <param name="dialogOwner">The dialog owner.</param>
        /// <param name="newContent">The new content.</param>
        /// <returns>The content to insert in the document.</returns>
        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string newContent)
        {
            using (new CursorKeeper(Cursors.WaitCursor))
            {
                var result = MessageBox.Show("Your latest starred links will be inserted. Continue?", "Link Builder Plugin", MessageBoxButtons.OKCancel);

                if (result == DialogResult.OK)
                {
                    SyndicationFeedFormatter feedFormatter = _options.FeedTypeOption == "Rss 2.0" ? (SyndicationFeedFormatter)new Rss20FeedFormatter() : new Atom10FeedFormatter();
                    newContent = BuildLinks(feedFormatter);
                }

                return result;
            }
        }

        /// <summary>
        /// Gets the files.
        /// </summary>
        /// <param name="path">The path to search.</param>
        /// <param name="fileExtension">The file extension.</param>
        /// <returns>A list of FileInfo objects matching the given file extension.</returns>
        private static IEnumerable<FileInfo> GetFiles(string path, string fileExtension)
        {
            var dirInfo = new DirectoryInfo(path);
            return dirInfo.GetFiles(string.Format("*{0}", fileExtension), SearchOption.TopDirectoryOnly).ToList();
        }

        /// <summary>
        /// Builds the links from an Atom feed.
        /// </summary>
        /// <param name="feedFormatter">The feed formatter.</param>
        /// <returns>A string containing the generated links.</returns>
        private string BuildLinks(SyndicationFeedFormatter feedFormatter)
        {
            string newContent = String.Empty;

            using (var reader = XmlReader.Create(String.Format("{0}?n={1}", _options.FeedUrlOption, _options.MaxPostsOption)))
            {
                var linkListing = new StringBuilder();

                if (feedFormatter != null && feedFormatter.CanRead(reader))
                {
                    feedFormatter.ReadFrom(reader);
                    var items = from item in feedFormatter.Feed.Items.OfType<SyndicationItem>()
                                where item.LastUpdatedTime.DateTime > GetLastBlogDate().AddHours(_options.BufferOption * -1)
                                orderby item.Authors.FirstOrDefault().Name, item.LastUpdatedTime.DateTime
                                select new BlogEntry
                                {
                                    Title = item.Title.Text,
                                    Url = item.Links.Count > 0 ? item.Links.FirstOrDefault().Uri.AbsoluteUri : String.Empty,
                                    Author = item.Authors.FirstOrDefault().Name
                                };

                    List<BlogEntry> blogEntries = items.ToList();

                    foreach (BlogEntry entry in blogEntries)
                    {
                        entry.AuthorInfo = GetBlogAuthor(entry.Author, entry.Url);

                        if (entry.AuthorInfo.DefaultCategory.Name == "misc")
                        {
                            entry.AuthorInfo.DefaultCategory = GetPostCategory(entry);
                        }
                    }

                    List<Category> categories = NameAndGetHeadings();
                    //var tableOfContents = new StringBuilder();
                    //tableOfContents.Append("<p>Jump to: ");
                    //linkListing.Append("<p>&#160;</p>");

                    categories.ForEach(category =>
                    {
                        //tableOfContents.Append(String.Format("<a href=\"http://www.alvinashcraft.com/#{0}\">{1}</a> | ", category.Name, category.Caption));
                        linkListing.Append(String.Format("<h3><a name=\"{0}\"></a>{1}</h3>", category.Name, category.Caption));

                        foreach (BlogEntry entry in blogEntries.Where(e => e.AuthorInfo.DefaultCategory.Name == category.Name))
                        {
                            linkListing.Append(String.Format("<p><a href=\"{0}\" target=\"_blank\">{1}</a> ({2})</p>", entry.Url, entry.Title, entry.AuthorInfo.AuthorName));
                        }

                        linkListing.Append("<p>&#160;</p>");
                    });

                    //tableOfContents.Remove(tableOfContents.Length - 2, 2);
                    //tableOfContents.Append("</p>");

                    //newContent = tableOfContents.ToString() + linkListing;
                    newContent = linkListing.ToString();
                }

                return newContent;
            }
        }

        /// <summary>
        /// Gets the post category.
        /// </summary>
        /// <param name="entry">The blog entry.</param>
        /// <returns></returns>
        private static Category GetPostCategory(BlogEntry entry)
        {
            if (entry.Title.ToLower().Contains("sharepoint"))
                return new Category(CategoryType.SharePoint);

            if (entry.Title.ToLower().Contains("silverlight") || entry.Title.ToLower().Contains("wpf") ||
                entry.Title.ToLower().Contains("windows phone") || entry.Title.ToLower().Contains("xaml") ||
                entry.Title.ToLower().Contains("mvvm") || entry.Title.ToLower().Contains("blend") ||
                entry.Title.ToLower().Contains("winrt") || entry.Title.ToLower().Contains("wp7") ||
                entry.Title.ToLower().Contains("metro"))
                return new Category(CategoryType.Xaml);

            if (entry.Title.ToLower().Contains("asp.net") || entry.Title.ToLower().Contains("mvc") ||
                entry.Title.ToLower().Contains("expression web") || entry.Title.ToLower().Contains("jquery") ||
                entry.Title.ToLower().Contains("javascript") || entry.Title.ToLower().Contains("css") ||
                entry.Title.ToLower().Contains("html") || entry.Title.ToLower().Contains("winjs") ||
                entry.Title.ToLower().Contains("signalr") || entry.Title.ToLower().Contains("windows azure") ||
                entry.Title.ToLower().Contains("kendo") || entry.Title.ToLower().Contains("php") ||
                entry.Title.ToLower().Contains("webstorm") || entry.Title.ToLower().Contains("web api") ||
                entry.Title.ToLower().Contains("knockout") || entry.Title.ToLower().Contains(".js"))
                return new Category(CategoryType.WebDevelopment);

            if (entry.Title.ToLower().Contains("sql server") || entry.Title.ToLower().Contains("ssis") ||
                entry.Title.ToLower().Contains("ssrs") || entry.Title.ToLower().Contains("dmv") || 
                entry.Title.ToLower().Contains("ssas") || entry.Title.ToLower().Contains("nosql") ||
                entry.Title.ToLower().Contains("sql compact") || entry.Title.ToLower().Contains("sql azure") ||
                entry.Title.ToLower().Contains("oracle"))
                return new Category(CategoryType.Sql);

            if (entry.Title.ToLower().Contains("cqrs") || entry.Title.ToLower().Contains("patterns") ||
                entry.Title.ToLower().Contains("agile") || entry.Title.ToLower().Contains("scrum") ||
                entry.Title.ToLower().Contains("kanban") || entry.Title.ToLower().Contains("tdd") ||
                entry.Title.ToLower().Contains("bdd") || entry.Title.ToLower().Contains("unit test") ||
                entry.Title.ToLower().Contains("estimation"))
                return new Category(CategoryType.Design);

            if (entry.Title.ToLower().Contains("android") || entry.Title.ToLower().Contains("xcode") ||
                entry.Title.ToLower().Contains("iphone") || entry.Title.ToLower().Contains("ipad") ||
                entry.Title.ToLower().Contains("ios"))
                return new Category(CategoryType.Mobile);

            if (entry.Title.ToLower().Contains("visual studio") || entry.Title.ToLower().Contains("vs2010") ||
                entry.Title.ToLower().Contains("c#") || entry.Title.ToLower().Contains("f#") ||
                entry.Title.ToLower().Contains("vb") || entry.Title.ToLower().Contains("linq") ||
                entry.Title.ToLower().Contains("wcf") || entry.Title.ToLower().Contains("tfs") ||
                entry.Title.ToLower().Contains("vs2012") || entry.Title.ToLower().Contains("resharper") ||
                entry.Title.ToLower().Contains("nuget"))
                return new Category(CategoryType.DotNet);

            return entry.AuthorInfo.DefaultCategory;
        }

        /// <summary>
        /// Gets the blog author.
        /// </summary>
        /// <param name="author">The author.</param>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        private static AuthorResult GetBlogAuthor(string author, string url)
        {
            string lowerAuthor = author.ToLower();
            string lowerUrl = url.ToLower();

            AuthorResult authorResult = CheckAuthorContains(lowerAuthor);

            if (!String.Equals(lowerAuthor, authorResult.AuthorName)) return authorResult;

            authorResult = FindExactAuthorMatch(lowerAuthor);

            if (!String.Equals(lowerAuthor, authorResult.AuthorName)) return authorResult;

            authorResult = CheckAuthorAndUrl(lowerAuthor, lowerUrl);

            if (!String.Equals(lowerAuthor, authorResult.AuthorName)) return authorResult;

            authorResult = CheckUrlContains(lowerAuthor, lowerUrl);

            if (!String.Equals(lowerAuthor, authorResult.AuthorName)) return authorResult;

            if (!String.IsNullOrEmpty(author) && !lowerAuthor.Contains("unknown"))
                return new AuthorResult(author, new Category());

            return new AuthorResult(lowerAuthor.Contains("unknown") ? "Unknown Author" : author, new Category());
        }

        /// <summary>
        /// Checks the author and URL.
        /// </summary>
        /// <param name="authorName">Name of the author.</param>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        private static AuthorResult CheckAuthorAndUrl(string authorName, string url)
        {
            switch (authorName)
            {
                case "don":
                    if (url.Contains("d4dotnet")) return new AuthorResult("Don Burnett", new Category(CategoryType.Xaml));
                    break;
                case "brian":
                    if (url.Contains("dbug")) return new AuthorResult("Brian Reindel", new Category());
                    break;
                case "charles":
                    if (url.Contains("channel9")) return new AuthorResult("Charles Torre", new Category(CategoryType.Podcasts));
                    break;
                case "arvind":
                    if (url.Contains("zoho")) return new AuthorResult("Arvind Natarajan", new Category());
                    break;
                case "christophe":
                    if (url.Contains("runningagile")) return new AuthorResult("Christophe Louvion", new Category(CategoryType.Design));
                    break;
                case "kathleen":
                    if (url.Contains("msmvps.com/blogs/kathleen")) return new AuthorResult("Kathleen Dollard", new Category(CategoryType.DotNet));
                    break;
                case "jd":
                    if (url.Contains("sourcesofinsight")) return new AuthorResult("J.D. Meier", new Category(CategoryType.Design));
                    break;
                case "blaine":
                    if (url.Contains("blogs.msdn.com/blaine")) return new AuthorResult("Blaine Wastell", new Category(CategoryType.Miscellaneous));
                    break;
                case "clarance":
                    if (url.Contains("zoho")) return new AuthorResult("Clarence Rozario", new Category());
                    break;
                case "daily":
                    if (url.Contains("onsaas")) return new AuthorResult("Jian Zhen", new Category(CategoryType.Links));
                    break;
                case "davembush":
                    return new AuthorResult("Dave M. Bush", new Category(CategoryType.WebDevelopment));
                case "major nelson":
                    return new AuthorResult("Larry Hyrb", new Category(CategoryType.Podcasts));
                case "scott":
                    if (url.Contains("roguetechnology")) return new AuthorResult("Scott Banwart", new Category(CategoryType.Links));
                    break;
                case "tobias":
                    if (url.Contains("powershell")) return new AuthorResult("Tobias Weltner", new Category(CategoryType.PowerShell));
                    break;
                case "alexanderb":
                    if (url.Contains("dzone")) return new AuthorResult("Alexander Beletsky", new Category(CategoryType.WebDevelopment));
                    break;
            }

            if (url.Contains("dmbcllc")) return new AuthorResult("Dave M. Bush", new Category(CategoryType.WebDevelopment));
            if (url.Contains("majornelsonblogcast")) return new AuthorResult("Larry Hyrb", new Category(CategoryType.Podcasts));

            return new AuthorResult(authorName, new Category());
        }

        /// <summary>
        /// Checks if the authorname field contains particular text and returns the correct name.
        /// </summary>
        /// <param name="authorName">Name of the author.</param>
        /// <returns></returns>
        private static AuthorResult CheckAuthorContains(string authorName)
        {
            if (authorName.Contains("sam gentile")) return new AuthorResult("Sam Gentile", new Category(CategoryType.Links));
            if (authorName.Contains("ayende")) return new AuthorResult("Oren Eini", new Category(CategoryType.DotNet));
            if (authorName.Contains("ray vernagus")) return new AuthorResult("Ray Vernagus", new Category());
            if (authorName.Contains("camerons")) return new AuthorResult("Cameron Skinner", new Category(CategoryType.DotNet));
            if (authorName.Contains("clemensreijnen")) return new AuthorResult("Clemens Reijnen", new Category(CategoryType.DotNet));
            if (authorName.Contains("jasonhaley")) return new AuthorResult("Jason Haley", new Category(CategoryType.Links));
            if (authorName.Contains("jschementi")) return new AuthorResult("Jimmy Schementi", new Category(CategoryType.DotNet));
            if (authorName.Contains("lapointe")) return new AuthorResult("Gary Lapointe", new Category());
            if (authorName.Contains("wildermuth")) return new AuthorResult("Shawn Wildermuth", new Category(CategoryType.Xaml));
            if (authorName.Contains("fervent coder")) return new AuthorResult("Rob Reynolds", new Category(CategoryType.DotNet));
            if (authorName.Contains("podwysocki")) return new AuthorResult("Matthew Podwysocki", new Category(CategoryType.DotNet));
            if (authorName.Contains("misfit geek") || authorName.Contains("joestagner")) return new AuthorResult("Joe Stagner", new Category(CategoryType.WebDevelopment));
            if (authorName.Contains("andrew stopford")) return new AuthorResult("Andy Stopford", new Category(CategoryType.Design));
            if (authorName.Contains("jopx")) return new AuthorResult("Joris Poelmans", new Category());
            if (authorName.Contains("beth massi")) return new AuthorResult("Beth Massi", new Category());
            if (authorName.Contains("the evil empire")) return new AuthorResult("Bertrand Le Roy", new Category(CategoryType.WebDevelopment));
            if (authorName.Contains("kltripp")) return new AuthorResult("Kimberly Tripp", new Category(CategoryType.Sql));
            if (authorName.Contains("powerscripting")) return new AuthorResult("Jonathan Walz & Hal Rottenberg", new Category(CategoryType.Podcasts));
            if (authorName.Contains("sharepoint pod show")) return new AuthorResult("Rob Foster, Nick Swan & Brett Lonsdale", new Category(CategoryType.Podcasts));
            if (authorName.Contains("michael kimsal")) return new AuthorResult("Michael Kimsal", new Category(CategoryType.WebDevelopment));
            if (authorName.Contains("marc grabanski")) return new AuthorResult("Marc Grabanski", new Category());
            if (authorName.Contains("david giard")) return new AuthorResult("David Giard", new Category(CategoryType.Podcasts));
            if (authorName.Contains("julian m bucknall")) return new AuthorResult("Julian M. Bucknall", new Category(CategoryType.WebDevelopment));

            return new AuthorResult(authorName, new Category());
        }

        /// <summary>
        /// Finds the exact author match.
        /// </summary>
        /// <param name="authorName">Name of the author.</param>
        /// <returns></returns>
        private static AuthorResult FindExactAuthorMatch(string authorName)
        {
            if (authorName == "brada") return new AuthorResult("Brad Abrams", new Category());
            if (authorName == "pinaldave") return new AuthorResult("Pinal Dave", new Category(CategoryType.Sql));
            if (authorName == "plblum") return new AuthorResult("Peter Blum", new Category());
            if (authorName == "wwagner") return new AuthorResult("Bill Wagner", new Category(CategoryType.DotNet));
            if (authorName == "anortham") return new AuthorResult("Alan Northam", new Category());
            if (authorName == "dotcore") return new AuthorResult("Dennis Delimarsky", new Category(CategoryType.DotNet));
            if (authorName == "traskjd") return new AuthorResult("JD Trask", new Category());
            if (authorName == "holoed") return new AuthorResult("Edmondo Pentangelo", new Category());
            if (authorName == "glenz") return new AuthorResult("Gunther Lenz", new Category(CategoryType.DotNet));
            if (authorName == "bruceky") return new AuthorResult("Bruce Kyle", new Category());
            if (authorName == "mitchp") return new AuthorResult("Mitch Pronschinske", new Category());
            if (authorName == "fredrik n") return new AuthorResult("Fredrik Normen", new Category(CategoryType.Xaml));
            if (authorName == "ekampf") return new AuthorResult("Eran Kampf", new Category());
            if (authorName == "sergejus") return new AuthorResult("Sergejus Barinovas", new Category());
            if (authorName == "zainnab") return new AuthorResult("Zain Naboulsi", new Category(CategoryType.DotNet));
            if (authorName == "wynapse") return new AuthorResult("Dave Campbell", new Category(CategoryType.Links));
            if (authorName == "xamlmammal") return new AuthorResult("Corey Miller", new Category(CategoryType.Xaml));
            if (authorName == "pete.brown" || authorName == "psychlist1972") return new AuthorResult("Pete Brown", new Category(CategoryType.Xaml));
            if (authorName == "rickm") return new AuthorResult("Rick Minerich", new Category(CategoryType.Links));
            if (authorName == "sergiopereira") return new AuthorResult("Sergio Pereira", new Category(CategoryType.DotNet));
            if (authorName == "tina10" || authorName == "tina") return new AuthorResult("Tina Wood", new Category(CategoryType.Podcasts));
            if (authorName == "andy@andybeaulieu.com") return new AuthorResult("Andy Beaulieu", new Category(CategoryType.Xaml));
            if (authorName == "spdblog") return new AuthorResult("SharePoint Designer Team", new Category(CategoryType.SharePoint));
            if (authorName == "pthurrott") return new AuthorResult("Paul Thurrott", new Category());
            if (authorName == "teichgraf") return new AuthorResult("Rene Schulte", new Category(CategoryType.Xaml));
            if (authorName == "hdierking") return new AuthorResult("Howard Dierking", new Category());
            if (authorName == "dsyme") return new AuthorResult("Don Syme", new Category());
            if (authorName == "emtucifor") return new AuthorResult("Erik", new Category());
            if (authorName == "davidlem") return new AuthorResult("David Lemphers", new Category());
            if (authorName == "david-starr") return new AuthorResult("David Starr", new Category());
            if (authorName == "udidahan") return new AuthorResult("Udi Dahan", new Category(CategoryType.Design));
            if (authorName == "jpelak") return new AuthorResult("John Pelak", new Category());
            if (authorName == "pnpguidance") return new AuthorResult("David Hayden", new Category(CategoryType.Design));
            if (authorName == "rob_relyea") return new AuthorResult("Rob Relyea", new Category(CategoryType.Xaml));
            if (authorName == "mbcrump") return new AuthorResult("Michael Crump", new Category(CategoryType.Xaml));
            if (authorName == "rruggeri") return new AuthorResult("Roberto Ruggeri", new Category());
            if (authorName == "scottgu") return new AuthorResult("Scott Guthrie", new Category(CategoryType.DotNet));
            if (authorName == "glucose") return new AuthorResult("Scott Hanselman", new Category(CategoryType.DotNet));
            if (authorName == "haacked") return new AuthorResult("Phil Haack", new Category(CategoryType.WebDevelopment));
            if (authorName == "schipps") return new AuthorResult("Sara J. Chipps", new Category());
            if (authorName == "maartenba") return new AuthorResult("Maarten Balliauw", new Category(CategoryType.Xaml));
            if (authorName == "zxue") return new AuthorResult("Zhiming Xue", new Category());
            if (authorName == "schambers") return new AuthorResult("Sean Chambers", new Category());
            if (authorName == "sacha") return new AuthorResult("Sacha Barber", new Category(CategoryType.Xaml));
            if (authorName == "azuidhof") return new AuthorResult("Arjan Zuidhof", new Category(CategoryType.Links));
            if (authorName == "daveburke") return new AuthorResult("Dave Burke", new Category(CategoryType.Links));
            if (authorName == "digimortal") return new AuthorResult("Gunnar Peipman", new Category(CategoryType.DotNet));
            if (authorName == "erobillard") return new AuthorResult("Eli Robillard", new Category());
            if (authorName == "goodidea") return new AuthorResult("yuanjian", new Category(CategoryType.Links));
            if (authorName == "habibh") return new AuthorResult("Habib Heydarian", new Category());
            if (authorName == "markhneedham") return new AuthorResult("Mark Needham", new Category(CategoryType.DotNet));
            if (authorName == "pkoen") return new AuthorResult("Peter Koen", new Category());
            if (authorName == "saraford") return new AuthorResult("Sara Ford", new Category());
            if (authorName == "sarahintampa") return new AuthorResult("Sarah Perez", new Category());
            if (authorName == "stefan_gossner") return new AuthorResult("Stefan Gossner", new Category());
            if (authorName == "sqlartist") return new AuthorResult("Trevor Dwyer", new Category(CategoryType.Sql));
            if (authorName == "derekgreer") return new AuthorResult("Derek Greer", new Category(CategoryType.Design));
            if (authorName == "uncle bob") return new AuthorResult("'Uncle Bob' Martin", new Category(CategoryType.Design));
            if (authorName == "steverowe") return new AuthorResult("Steve Rowe", new Category());
            if (authorName == "simonech") return new AuthorResult("Simone Chiaretta", new Category(CategoryType.WebDevelopment));
            if (authorName == "matt-milner") return new AuthorResult("Matt Milner", new Category());
            if (authorName == "wzack") return new AuthorResult("Bill Zack", new Category());
            if (authorName == "kirke") return new AuthorResult("Kirk Evans", new Category());
            if (authorName == "davedev") return new AuthorResult("Dave Isbitski", new Category());
            if (authorName == "mtaulty") return new AuthorResult("Mike Taulty", new Category(CategoryType.DotNet));
            if (authorName == "sashag") return new AuthorResult("Sasha Goldshtein", new Category(CategoryType.DotNet));
            if (authorName == "sqldenis") return new AuthorResult("Denis Gobo", new Category(CategoryType.Sql));
            if (authorName == "bogardj") return new AuthorResult("Jimmy Bogard", new Category(CategoryType.WebDevelopment));
            if (authorName == "alkampfer") return new AuthorResult("Gian Maria Ricci", new Category());
            if (authorName == "mikeormond") return new AuthorResult("Mike Ormond", new Category(CategoryType.DotNet));
            if (authorName == "shair") return new AuthorResult("Shai Raiten", new Category(CategoryType.DotNet));
            if (authorName == "crpietschmann") return new AuthorResult("Chris Pietschmann", new Category(CategoryType.DotNet));
            if (authorName == "delay") return new AuthorResult("David Anson", new Category(CategoryType.Xaml));
            if (authorName == "derick.bailey") return new AuthorResult("Derick Bailey", new Category(CategoryType.Design));
            if (authorName == "gduthie" || authorName == "devhammer") return new AuthorResult("G. Andrew Duthie", new Category(CategoryType.Community));
            if (authorName == "jesseliberty") return new AuthorResult("Jesse Liberty", new Category(CategoryType.Xaml));
            if (authorName == "danielfe") return new AuthorResult("Dan Fernandez", new Category());
            if (authorName == "frankarr") return new AuthorResult("Frank Arrigo", new Category());
            if (authorName == "kimspilker") return new AuthorResult("Kim Spilker", new Category());
            if (authorName == "piccoloprincipe") return new AuthorResult("Giorgio Sironi", new Category(CategoryType.WebDevelopment));
            if (authorName == "onpnt") return new AuthorResult("Ted Krueger", new Category(CategoryType.Sql));
            if (authorName == "simonsabin") return new AuthorResult("Simon Sabin", new Category(CategoryType.Sql));
            if (authorName == "jsonmez") return new AuthorResult("John Sonmez", new Category(CategoryType.Design));
            if (authorName == "peli de halleux") return new AuthorResult("Jonathan de Halleux", new Category(CategoryType.Design));
            if (authorName == "angelab") return new AuthorResult("Angela Binkowski", new Category(CategoryType.DotNet));
            if (authorName == "sptblog") return new AuthorResult("SharePoint Team", new Category(CategoryType.SharePoint));
            if (authorName == "keyvan@nayyeri.net" || authorName == "keyvan") return new AuthorResult("Keyvan Nayyeri", new Category(CategoryType.WebDevelopment));
            if (authorName == "james.kovacs") return new AuthorResult("James Kovacs", new Category(CategoryType.Design));
            if (authorName == "mbratton") return new AuthorResult("Marcus Bratton", new Category());
            if (authorName == "lmoroney@microsoft.com") return new AuthorResult("Lawrence Moroney", new Category(CategoryType.Xaml));
            if (authorName == "jprosise") return new AuthorResult("Jeff Prosise", new Category(CategoryType.Xaml));
            if (authorName == "deborahk") return new AuthorResult("Deborah Kurata", new Category(CategoryType.Xaml));
            if (authorName == "jamesbrundage") return new AuthorResult("James Brundage", new Category());
            if (authorName == "heratech") return new AuthorResult("Julie Stickler", new Category(CategoryType.DotNet));
            if (authorName == "jayneu") return new AuthorResult("Jay Esguerra", new Category());
            if (authorName == "elijahmanor") return new AuthorResult("Elijah Manor", new Category(CategoryType.Links));
            if (authorName == "david hill's weblog") return new AuthorResult("David Hill", new Category());
            if (authorName == "gojko") return new AuthorResult("Gojko Adzic", new Category(CategoryType.Design));
            if (authorName == "jamiet" || authorName == "jamie.thomson") return new AuthorResult("Jamie Thomson", new Category());
            if (authorName == "xnablog") return new AuthorResult("Kathleen Sanders", new Category(CategoryType.Links));
            if (authorName == "gregorylong") return new AuthorResult("Gregory Long", new Category());
            if (authorName == "bhitney") return new AuthorResult("Brian Hitney", new Category());
            if (authorName == "vbandi") return new AuthorResult("András Velvárt", new Category(CategoryType.Xaml));

            return new AuthorResult(authorName, new Category());
        }

        /// <summary>
        /// Checks the URL contains.
        /// </summary>
        /// <param name="authorName">Name of the author.</param>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        private static AuthorResult CheckUrlContains(string authorName, string url)
        {
            if (url.Contains("bhandler")) return new AuthorResult("Blake Handler", new Category());
            if (url.Contains("dougholland")) return new AuthorResult("Doug Holland", new Category());
            if (url.Contains("mikeormond")) return new AuthorResult("Mike Ormond", new Category());
            if (url.Contains("agilescout")) return new AuthorResult("Peter Saddington", new Category(CategoryType.Design));
            if (url.Contains("codinghorror")) return new AuthorResult("Jeff Atwood", new Category());
            if (url.Contains("devhammer")) return new AuthorResult("G. Andrew Duthie", new Category(CategoryType.Community));
            if (url.Contains("codingthearchitecture")) return new AuthorResult("Simon Brown", new Category(CategoryType.Design));
            if (url.Contains("www.wduffy.co.uk")) return new AuthorResult("William Duffy", new Category());
            if (url.Contains("pixel8")) return new AuthorResult("Craig Shoemaker", new Category(CategoryType.Podcasts));
            if (url.Contains("geekswithblogs.net/ranganh")) return new AuthorResult("Harish Ranganathan", new Category());
            if (url.Contains("maartenballiauw")) return new AuthorResult("Maarten Balliauw", new Category(CategoryType.WebDevelopment));
            if (url.Contains("electricbeach")) return new AuthorResult("Christian Schormann", new Category(CategoryType.Xaml));
            if (url.Contains("4guysfrom")) return new AuthorResult("Scott Mitchell", new Category(CategoryType.WebDevelopment));
            if (url.Contains("highoncoding.com")) return new AuthorResult("Mohammad Azam", new Category());
            if (url.Contains("charliedigital")) return new AuthorResult("Charles Chen", new Category());
            if (url.Contains("jamesshore")) return new AuthorResult("James Shore", new Category(CategoryType.Design));
            if (url.Contains("nikhilk")) return new AuthorResult("Nikhil Kothari", new Category(CategoryType.Xaml));
            if (url.Contains("windowsphonegeek")) return new AuthorResult("Windows Phone Geek", new Category(CategoryType.Xaml));
            if (url.Contains("deepfriedbytes")) return new AuthorResult("Keith Elder & Chris Woodruff", new Category(CategoryType.Podcasts));
            if (url.Contains("wekeroad")) return new AuthorResult("Rob Conery", new Category(CategoryType.WebDevelopment));
            if (url.Contains("peterkellnernet")) return new AuthorResult("Peter Kellner", new Category());
            if (url.Contains("ayende")) return new AuthorResult("Oren Eini", new Category(CategoryType.DotNet));
            if (url.Contains("reedcopsey")) return new AuthorResult("Reed Copsey", new Category(CategoryType.DotNet));
            if (url.Contains("winsupersite")) return new AuthorResult("Paul Thurrott", new Category());
            if (url.Contains("roryprimrose")) return new AuthorResult("Rory Primrose", new Category(CategoryType.DotNet));
            if (url.Contains("microsoft.com/events/podcasts")) return new AuthorResult("MS Podcasts", new Category(CategoryType.Podcasts));
            if (url.Contains("sqlskills.com")) return new AuthorResult("Paul S. Randal", new Category(CategoryType.Sql));
            if (url.Contains("dotnetkicks")) return new AuthorResult("DotNetKicks", new Category());
            if (url.Contains("microsoftdownload") || url.Contains("msdownloads")) return new AuthorResult("MS Downloads", new Category(CategoryType.Miscellaneous));
            if (url.Contains("lazycoder")) return new AuthorResult("Scott Koon", new Category());
            if (url.Contains("kirupa")) return new AuthorResult("Kirupa Chinnathambi", new Category(CategoryType.Xaml));
            if (url.Contains("grantpalin")) return new AuthorResult("Grant Palin", new Category(CategoryType.Links));
            if (url.Contains("webbtechsolutions")) return new AuthorResult("Joe Webb", new Category(CategoryType.Sql));
            if (url.Contains("thedatafarm")) return new AuthorResult("Julie Lerman", new Category(CategoryType.DotNet));
            if (url.Contains("firstfloorsoftware")) return new AuthorResult("Koen Zwikstra", new Category(CategoryType.Xaml));
            if (url.Contains("mistergoodcat")) return new AuthorResult("Peter Kuhn", new Category(CategoryType.Xaml));
            if (url.Contains("rudigrobler")) return new AuthorResult("Rudi Grobler", new Category(CategoryType.Xaml));
            if (url.Contains("japf.fr")) return new AuthorResult("Jeremy Alles", new Category(CategoryType.Xaml));
            if (url.Contains("stevegilham")) return new AuthorResult("Steve Gilham", new Category());
            if (url.Contains("jasonbock")) return new AuthorResult("Jason Bock", new Category(CategoryType.Links));
            if (url.Contains("hanselminutes")) return new AuthorResult("Scott Hanselman", new Category(CategoryType.Podcasts));
            if (url.Contains("programmingtour")) return new AuthorResult("Corey Haines", new Category(CategoryType.Design));
            if (url.Contains("paul-zubkov")) return new AuthorResult("Paul Zubkov", new Category());
            if (url.Contains("thekua")) return new AuthorResult("Patrick Kua", new Category(CategoryType.Design));
            if (url.Contains("kevingriffin")) return new AuthorResult("Kevin Griffin", new Category());
            if (url.Contains("robmiles")) return new AuthorResult("Rob Miles", new Category());
            if (url.Contains("ariankulp")) return new AuthorResult("Arian Kulp", new Category());
            if (url.Contains("gweek.libsyn")) return new AuthorResult("Mark Frauenfelder", new Category(CategoryType.Podcasts));
            if (url.Contains("windowsphonedevpodcast")) return new AuthorResult("Ryan Lowdermilk & Travis Lowdermilk", new Category(CategoryType.Podcasts));
            if (url.Contains("netcave.org")) return new AuthorResult("Alan Stevens", new Category());
            if (url.Contains("codingqa")) return new AuthorResult("Mark Berryman & Jim Wang", new Category(CategoryType.Podcasts));
            if (url.Contains("wpf.2000things")) return new AuthorResult("Sean Sexton", new Category(CategoryType.Xaml));
            if (url.Contains("developingfor.net")) return new AuthorResult("Joel Cochran", new Category(CategoryType.Xaml));
            if (url.Contains("martinfowler.com")) return new AuthorResult("Martin Fowler", new Category(CategoryType.Design));
            if (url.Contains("kirkk.com")) return new AuthorResult("Kirk Knoernschild", new Category(CategoryType.Design));
            if (url.Contains("dotnetrocks")) return new AuthorResult("Carl Franklin & Richard Campbell", new Category(CategoryType.Podcasts));
            if (url.Contains("nerdyhearn")) return new AuthorResult("Tom Hearn", new Category(CategoryType.WebDevelopment));
            if (url.Contains("jsmag")) return new AuthorResult("Michael Kimsal", new Category(CategoryType.WebDevelopment));
            if (url.Contains("tess") && url.Contains("msdn")) return new AuthorResult("Tess Ferrandez", new Category(CategoryType.DotNet));
            if (url.Contains("whereslou")) return new AuthorResult("Louis DeJardin", new Category(CategoryType.WebDevelopment));
            if (url.Contains("dotnet.org.za/rudi") || url.Contains("rudigrobler")) return new AuthorResult("Rudi Grobler", new Category(CategoryType.Xaml));
            if (url.Contains("platinumbay")) return new AuthorResult("Steve Andrews", new Category(CategoryType.DotNet));
            if (url.Contains("rtipton.wordpress")) return new AuthorResult("Rhonda Tipton", new Category(CategoryType.Links));
            if (url.Contains("coderant")) return new AuthorResult("Mike Hadlow", new Category(CategoryType.DotNet));
            if (url.Contains("herdingcode")) return new AuthorResult("Jon Galloway", new Category(CategoryType.Podcasts));
            if (url.Contains("coolthingoftheday")) return new AuthorResult("Greg Duncan", new Category());
            if (url.Contains("charlespetzold")) return new AuthorResult("Charles Petzold", new Category(CategoryType.Xaml));
            if (url.Contains("blackwasp")) return new AuthorResult("Richard Carr", new Category(CategoryType.DotNet));
            if (url.Contains("trelford")) return new AuthorResult("Phillip Trelford", new Category());
            if (url.Contains("openmymind.net")) return new AuthorResult("Karl Seguin", new Category(CategoryType.WebDevelopment));

            return new AuthorResult(authorName, new Category());
        }

        /// <summary>
        /// Gets the last blog date.
        /// </summary>
        /// <returns>Date/Time of the last blog posting.</returns>
        private DateTime GetLastBlogDate()
        {
            IEnumerable<FileInfo> theFiles = GetFiles(_options.PostPathOption, ".wpost");

            var files = from file in theFiles
                        where file.Name.Contains(_options.PostPrefixOption)
                        orderby file.CreationTime descending
                        select file;

            List<FileInfo> matchingFiles = files.ToList();

            return matchingFiles.Count > 0 ? matchingFiles[0].CreationTime : DateTime.Now.AddDays(-1);
        }

        /// <summary>
        /// Names and gets the category headings.
        /// </summary>
        /// <returns></returns>
        private static List<Category> NameAndGetHeadings()
        {
            var categories = new List<Category>
                                 {
                                     new Category(CategoryType.Top),
                                     new Category(CategoryType.WebDevelopment),
                                     new Category(CategoryType.Xaml),
                                     new Category(CategoryType.DotNet),
                                     new Category(CategoryType.Design),
                                     new Category(CategoryType.Mobile),
                                     new Category(CategoryType.Podcasts),
                                     new Category(CategoryType.Community),
                                     new Category(CategoryType.Sql),
                                     new Category(CategoryType.SharePoint),
                                     new Category(CategoryType.PowerShell),
                                     new Category(CategoryType.Miscellaneous),
                                     new Category(CategoryType.Links),
                                     new Category(CategoryType.Shelf)
                                 };

            return categories;
        }
    }
}
